//package com.cema.manage.common;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import com.cema.manage.common.utils.QiniuSimpleUpload;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Controller;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.ResponseBody;
//import org.springframework.web.multipart.MultipartFile;
//import org.springframework.web.multipart.MultipartHttpServletRequest;
//import org.springframework.web.multipart.commons.CommonsMultipartResolver;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.util.Iterator;
//
//@Controller
//@RequestMapping("/manage/upload")
//public class UploadController {
//    @Value("${qiniu.ym}")
//    private String getPath;
//    @Autowired
//    private QiniuSimpleUpload qiniuSimpleUpload;
//
//    @RequestMapping("/getToken")
//    @ResponseBody
//    public JSONObject getToken(HttpServletRequest request) {
//        JSONObject jsonObject = new JSONObject();
//        return jsonObject;
//
//    }
//    @RequestMapping("/upload")
//    @ResponseBody
//    public String upLoad(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        JSONObject jsonObject = new JSONObject();
//        try {
//            CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
//                    request.getSession().getServletContext());
//            if (multipartResolver.isMultipart(request)) {
//                //将request变成多部分request
//                MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
//                //获取multiRequest 中所有的文件名
//                Iterator iter = multiRequest.getFileNames();
//
//                while (iter.hasNext()) {
//                    //一次遍历所有文件
//                    MultipartFile file = multiRequest.getFile(iter.next().toString());
//                    //上传图片
//                    if (file != null) {
//                        String upResult = qiniuSimpleUpload.upload(file.getBytes(), QiniuSimpleUpload.getUUID());
//                        JSONObject formatUpResult = JSON.parseObject(upResult);
//                        if (formatUpResult.containsKey("key")) {
//                            jsonObject.put("url", getPath + "/" + formatUpResult.get("key").toString());
//                            jsonObject.put("code", "000000");
//                            return jsonObject.toString();
//                        }
//                        jsonObject.put("code", "100000");
//                        jsonObject.put("msg", "上传失败");
//                        return jsonObject.toString();
//                    }
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        jsonObject.put("code", "100000");
//        jsonObject.put("msg", "上传失败");
//        return jsonObject.toString();
//    }
//}
